#### Start of system configuration section. ####

srcdir =	@srcdir@
VPATH =		@srcdir@

# Common prefix for machine-independent installed files.
prefix =	@prefix@

# Common prefix for machine-dependent installed files.
exec_prefix =	@exec_prefix@

bindir =	$(exec_prefix)/bin
libdir =	$(exec_prefix)/lib
syslibdir =	$(exec_prefix)/Sys/libs
incdir =	$(exec_prefix)/include

INSTALL =	@INSTALL@
INSTALL_DATA =	@INSTALL_DATA@

CC =		@CC@
CFLAGS =	@CFLAGS@
LDFLAGS =	@LDFLAGS@

RANLIB =	@RANLIB@

#### End system configuration section ####

# Default is generic m68k version with no FPU support.

CPU =		68000
FPU =		soft-float
BASE =		no-baserel

# Some additional options for OTHER_CFLAGS that may or may not be useful are:
#
# -funroll-loops
# -fforce-mem
# -DDEBUG_VERSION	(build a debugging version)

#Use the flag to suppress stack extension when 2.6.4/2.7 is installed.
#STACKEXTEND =	-mnostackextend
OTHER_CFLAGS =	-fomit-frame-pointer $(STACKEXTEND)

BASE_FLAGS_TO_PASS = \
		"BASE=$(BASE)" \
		"BUILDDIR=$(BUILDDIR)" \
		"CC=$(CC)" \
		"CFLAGS=$(CFLAGS)" \
		"CPU=$(CPU)" \
		"FPU=$(FPU)" \
		"LDFLAGS=$(LDFLAGS)" \
		"OTHER_CFLAGS=$(OTHER_CFLAGS)" \
		"RANLIB=$(RANLIB)"

FLAGS_TO_PASS =	$(BASE_FLAGS_TO_PASS) $(EXTRA_FLAGS_TO_PASS)

###  The default thing to do is to make all reasonable combinations of the
###  library.
###
###  Note that libsrc, which builds the runtime startup files and both
###  versions of libc.a, completely ignores the FPU/CPU/BASE variables,
###  so we just call it directly.  Also note that the 68000 versions
###  of the files MUST be built before libsrc.
###
###  Other than the restriction noted above, you can comment out specific
###  CPU/FPU combinations that you are not interested in.  However you
###  do have to build both the baserel and no-baserel versions for the
###  combinations you want to build.

all:
		$(MAKE) build CPU=68000 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68000 FPU=soft-float BASE=baserel
		$(MAKE) build CPU=68020 FPU=68881      BASE=no-baserel
		$(MAKE) build CPU=68020 FPU=68881      BASE=baserel
		$(MAKE) build CPU=68020 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68020 FPU=soft-float BASE=baserel
		$(MAKE) build CPU=68030 FPU=68881      BASE=no-baserel
		$(MAKE) build CPU=68030 FPU=68881      BASE=baserel
		$(MAKE) build CPU=68030 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68030 FPU=soft-float BASE=baserel
		$(MAKE) build CPU=68040 FPU=68881      BASE=no-baserel
		$(MAKE) build CPU=68040 FPU=68881      BASE=baserel
		$(MAKE) build CPU=68040 FPU=soft-float BASE=no-baserel
		$(MAKE) build CPU=68040 FPU=soft-float BASE=baserel
		@(cd libsrc && $(MAKE) $(FLAGS_TO_PASS))
		@(cd ixtrace && $(MAKE) $(FLAGS_TO_PASS))

###  The "build" target makes a single flavor of the library.  I.E.
###  the 68040+FPU version.

build:
		@$(MAKE) gen_library-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) glue-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) gnulib-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) gnulib-$(FPU)-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) network-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) stack-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) static-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) stdio_2-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) stdlib-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) string-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)
		@$(MAKE) library-$(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS)

###  These targets are used to map a generic target <builddir>-<BASE>-<CPU>-<FPU>
###  to one of the less complicated targets.  I.E. if something only needs to
###  be built for a particular CPU and FPU, and the value of BASE is irrelevant,
###  then it would be mapped to <CPU>-<FPU>.

gen_library-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=gen_library

glue-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE) $(FLAGS_TO_PASS) BUILDDIR=glue

gnulib-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=gnulib

gnulib-$(FPU)-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=gnulib-$(FPU)

library-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=library

network-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=network

stack-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=stack

static-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(BASE)-$(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=static

stdio_2-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=stdio_2

stdlib-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=stdlib

string-$(BASE)-$(CPU)-$(FPU):
		@$(MAKE) $(CPU)-$(FPU) $(FLAGS_TO_PASS) BUILDDIR=string

###  These targets are the ones that are reduced from the full generic
###  targets, on a per directory basis.  I.E. if it is only necessary
###  to build versions for a specific BASE and the CPU/FPU combination
###  is irrelevant, then the generic target is mapped to just BASE.

# This target is for things that are only dependent on BASE.

$(BASE):
		@if [ -d $(BUILDDIR)/$(BASE) ]; then \
			true; \
		else \
			echo mkdir -p $(BUILDDIR)/$(BASE); \
			mkdir -p $(BUILDDIR)/$(BASE); \
		fi;
		@(cd $(BUILDDIR)/$(BASE) ; $(MAKE) -f ../Makefile $(FLAGS_TO_PASS));

# This target is for things that are only dependent on CPU and FPU.

$(CPU)-$(FPU):
		@if [ -d $(BUILDDIR)/$(CPU)/$(FPU) ]; then \
			true; \
		else \
			echo mkdir -p $(BUILDDIR)/$(CPU)/$(FPU); \
			mkdir -p $(BUILDDIR)/$(CPU)/$(FPU); \
		fi;
		@(cd $(BUILDDIR)/$(CPU)/$(FPU) ; $(MAKE) -f ../../Makefile $(FLAGS_TO_PASS))

# This target is for things that are dependent on CPU, FPU, and BASE.

$(BASE)-$(CPU)-$(FPU):
		@if [ -d $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU) ]; then \
			true; \
		else \
			echo mkdir -p $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU); \
			mkdir -p $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU); \
		fi;
		@(cd $(BUILDDIR)/$(BASE)/$(CPU)/$(FPU) ; $(MAKE) -f ../../../Makefile $(FLAGS_TO_PASS));

###  Install all the libraries, include files, runtime files, etc.

install:
		$(INSTALL) library/68000/soft-float/ixconfig $(bindir)/ixconfig
		$(INSTALL) library/68000/soft-float/ixemul.library $(syslibdir)/ixemul.library
		$(INSTALL) library/68000/soft-float/ixemul.trace $(syslibdir)/ixemul.trace
		$(INSTALL) library/68000/soft-float/ixemul.library $(syslibdir)/ixemul000.library
		$(INSTALL) library/68000/soft-float/ixemul.trace $(syslibdir)/ixemul000.trace
		$(INSTALL) library/68020/soft-float/ixemul.library $(syslibdir)/ixemul020.library
		$(INSTALL) library/68020/soft-float/ixemul.trace $(syslibdir)/ixemul020.trace
		$(INSTALL) library/68030/soft-float/ixemul.library $(syslibdir)/ixemul030.library
		$(INSTALL) library/68030/soft-float/ixemul.trace $(syslibdir)/ixemul030.trace
		$(INSTALL) library/68040/soft-float/ixemul.library $(syslibdir)/ixemul040.library
		$(INSTALL) library/68040/soft-float/ixemul.trace $(syslibdir)/ixemul040.trace
		$(INSTALL) library/68020/68881/ixemul.library $(syslibdir)/ixemul020fpu.library
		$(INSTALL) library/68020/68881/ixemul.trace $(syslibdir)/ixemul020fpu.trace
		$(INSTALL) library/68030/68881/ixemul.library $(syslibdir)/ixemul030fpu.library
		$(INSTALL) library/68030/68881/ixemul.trace $(syslibdir)/ixemul030fpu.trace
		$(INSTALL) library/68040/68881/ixemul.library $(syslibdir)/ixemul040fpu.library
		$(INSTALL) library/68040/68881/ixemul.trace $(syslibdir)/ixemul040fpu.trace
		$(INSTALL) libsrc/bcrt0.o $(libdir)/bcrt0.o
		$(INSTALL) libsrc/crt0.o $(libdir)/crt0.o
		$(INSTALL) libsrc/rcrt0.o $(libdir)/rcrt0.o
		$(INSTALL) libsrc/libc.a $(libdir)/libc.a
		$(INSTALL) libsrc/libbc.a $(libdir)/libb/libc.a
		$(INSTALL) ixtrace/ixtrace $(bindir)/ixtrace
		(cd $(srcdir)/include && cp -r . $(incdir))
		
clean:
		(cd gen_library ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd gnulib ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd gnulib-soft-float ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd gnulib-68881 ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd library ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd stdio_2 ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd stdlib ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd string ; $(MAKE) $(FLAGS_TO_PASS) clean)
		(cd network ; $(MAKE) $(FLAGS_TO_PASS) clean)

# Build source and binary distribution files.

dist:
		#
		# First get rid of any leftovers from a previous "make dist"
		#
		rm -rf gnu ixemul-bin *.lha
		#
		# Create ixemul-XX.YY-bin.lha, which contains utilities
		# that are generally useful to ixemul.library users.
		#
		mkdir -p gnu/bin
		cp -p library/68000/soft-float/ixconfig gnu/bin/ixconfig
		cp -p ixtrace/ixtrace gnu/bin/ixtrace
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-bin.lha bin)
		sleep 10
		rm -rf gnu
		#
		# Create ixemul-XX.YY-sdk.lha, which contains the files that
		# programmers need to build ixemul.library using applications.
		#
		mkdir -p gnu/lib/libb
		cp -p libsrc/bcrt0.o gnu/lib/bcrt0.o
		cp -p libsrc/crt0.o gnu/lib/crt0.o
		cp -p libsrc/rcrt0.o gnu/lib/rcrt0.o
		cp -p libsrc/libc.a gnu/lib/libc.a
		cp -p libsrc/libbc.a gnu/lib/libb/libc.a
		mkdir -p gnu/include
		cp -pr $(srcdir)/include gnu
		mkdir -p gnu/man
		cp -pr $(srcdir)/man gnu
		rm -f gnu/man/Makefile
		rm -f gnu/man/makewhatis.sed
		rm -f gnu/man/whatis.db
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-sdk.lha lib include man)
		sleep 10
		rm -rf gnu
		#
		# Create ixemul-XX.YY-doc.lha, which contains various documentation
		# files.
		#
		mkdir ixemul-bin
		cp -p $(srcdir)/COPYING ixemul-bin/COPYING
		cp -p $(srcdir)/COPYING.LIB ixemul-bin/COPYING.LIB
		cp -p $(srcdir)/INSTALL ixemul-bin/INSTALL
		cp -p $(srcdir)/NEWS ixemul-bin/NEWS
		cp -p $(srcdir)/BUGS ixemul-bin/BUGS
		cp -p $(srcdir)/TODO ixemul-bin/TODO
		cp -p $(srcdir)/docs/README ixemul-bin/README
		cp -p $(srcdir)/ixtrace/README ixemul-bin/README.ixtrace
		lha -mraxeq a `basename $(srcdir)`-doc.lha ixemul-bin
		rm -rf ixemul-bin
		#
		# Create ixemul-XX.YY-src.lha, which contains the source code.
		# Do it in a temporary location so we don't have to
		# know where the build dir is, or translate between
		# Unix & AmigaDOS forms.
		#
		rm -f /tmp/ixemul-src.lha
		(cd $(srcdir)/.. && lha -mraxeq a tmp:ixemul-src.lha `basename $(srcdir)`)
		cp /tmp/ixemul-src.lha `basename $(srcdir)`-src.lha
		rm -f /tmp/ixemul-src.lha
		#
		# Create ixemul-XX.YY-<cpu><fpu>.lha
		#
		mkdir -p gnu/Sys/libs
		cp -p library/68000/soft-float/ixemul.library gnu/Sys/libs/ixemul000.library
		cp -p library/68000/soft-float/ixemul.trace gnu/Sys/libs/ixemul000.trace
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-0s.lha Sys)
		rm -f gnu/Sys/libs/*
		cp -p library/68020/soft-float/ixemul.library gnu/Sys/libs/ixemul020.library
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-2s.lha Sys)
		rm -f gnu/Sys/libs/*
		cp -p library/68030/soft-float/ixemul.library gnu/Sys/libs/ixemul030.library
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-3s.lha Sys)
		rm -f gnu/Sys/libs/*
		cp -p library/68040/soft-float/ixemul.library gnu/Sys/libs/ixemul040.library
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-4s.lha Sys)
		rm -f gnu/Sys/libs/*
		cp -p library/68020/68881/ixemul.library gnu/Sys/libs/ixemul020fpu.library
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-2f.lha Sys)
		rm -f gnu/Sys/libs/*
		cp -p library/68030/68881/ixemul.library gnu/Sys/libs/ixemul030fpu.library
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-3f.lha Sys)
		rm -f gnu/Sys/libs/*
		cp -p library/68040/68881/ixemul.library gnu/Sys/libs/ixemul040fpu.library
		(cd gnu && lha -mraxeq a /`basename $(srcdir)`-4f.lha Sys)
		sleep 10
		rm -f gnu
